<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<div class="main-content">
    <div class="box box-main">
        <form id="inputForm" model="post" action="<c:url value="/web/admin/personnel/addOrUpdate.action"/>"
              method="post" enctype="multipart/form-data" class="form-horizontal">
            <div class="box-body">
                <div class="form-unit">基本信息</div>
                <input name="id" value="${personnel.id}" type="hidden">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 所属部门：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="organizationValue" name="organizationId" class="form-control required"/>
                                  <%--  <c:forEach items="${organizationList}" var="organization">
                                        <option
                                                <c:if test="${organization.id==personnel.organizationId}">selected</c:if>
                                                value="${organization.id}">${organization.organizationName}</option>
                                    </c:forEach>
                                </select>--%>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 手机号：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="postCode111" id="mobile" name="mobile" value="${personnel.mobile}"<c:if
                                        test="${personnel==null}"> onblur="findPersonnel();"</c:if>
                                       <c:if test="${personnel!=null}">readonly </c:if>
                                       maxlength="64" class="form-control required <c:if test="${personnel==null}">mobile</c:if>"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 姓名：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="postCode111" id="userName" name="name" value="${personnel.name}"
                                       maxlength="64" class="form-control required"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 性别：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="sex" type="radio" name="sex" value="男" checked >男
                                <input type="radio" name="sex" value="女" <c:if test="${personnel.sex=='女'}">checked</c:if>>女
<%--
                                <select id="sex" name="sex" class="form-control required">
                                    <option value="男" selected>男</option>
                                    <option value="女" <c:if test="${personnel.sex=='女'}">selected</c:if>> 女</option>
                                </select>--%>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required "></span> 工号：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="postCode111" id="employeeCode" name="employeeCode"
                                       value="${personnel.employeeCode}"
                                       maxlength="64" class="form-control "/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <%--<span class="required ">*</span>--%> 邮箱：<i
                                    class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="postCode111" id="email" name="email" value="${personnel.email}"
                                       maxlength="64" class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 所属岗位：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="positionId" name="positionId" class="form-control required"/>
                               <%--     <c:forEach items="${positionList}" var="position">
                                        <option
                                                <c:if test="${position.id==personnel.positionId}">selected</c:if>
                                                value="${position.id}">${position.name}</option>
                                    </c:forEach>
                                </input>--%>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <%--<span class="required ">*</span>--%> 所属区域：<i
                                    class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <select id="id_select" name="areaIds" class="selectpicker bla bla bli form-control" multiple
                                        data-live-search="true">
                                    <c:forEach items="${areaList}" var="area">
                                        <option value="${area.id}">${area.areaName}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <%--<span class="required ">*</span>--%> 是否为部门负责人：<i
                                    class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="leader" type="radio" name="leader" value="0" checked >否 <input type="radio" name="leader" value="1" <c:if test="${personnel.leader=='true'}">checked</c:if>>是
                               <%-- <select id="leader" name="leader" class=" form-control">
                                    <option value="0" selected>否</option>
                                    <option value="1" <c:if test="${personnel.leader=='true'}">selected</c:if>> 是</option>
                                </select>--%>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            <div class="box-footer">
                <div class="row">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i>
                            保 存
                        </button>&nbsp;

                        <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="closeView()"><i
                                class="fa fa-times"></i> 关 闭
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var organizationId = $('#organizationValue').magicSuggest({
        allowFreeEntries: false,
        data:${organizationList},
        queryParam: 'q',
        valueField: "id",
        displayField: "organizationName",
        maxSelection: 1,
        noSuggestionText: '',
        placeholder: '请选择',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: magicSuggestValue('${personnel.organizationId}')
    });
    var positionId = $('#positionId').magicSuggest({
        allowFreeEntries: false,
        data:${positionList},
        queryParam: 'q',
        valueField: "id",
        displayField: "name",
        maxSelection: 1,
        noSuggestionText: '',
        placeholder: '请选择',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: magicSuggestValue('${personnel.positionId}')
    });

    $(organizationId).on('selectionchange', function(e,m){
        validateMagicReload();
    });
    $(positionId).on('selectionchange', function(e,m){
        validateMagicReload();
    });
    $('.selectpicker').selectpicker({
        'noneSelectedText': '请选择区域'
    });
    $('.selectpicker').selectpicker('val', ${personnel.areaIds});
    $("#inputForm").validate({
        submitHandler: function (form) {
            if (!validateMagicSuggest({
                organizationId: '不能为空',
                positionId: '不能为空'
            })) return false;
            js.ajaxSubmitForm($(form), function (data) {
                // console.log(data);
                js.showMessage(data.msg);
                if (data.status == Global.NO) {
                    closeView();
                    $('#dataGrid').dataGrid("refresh");
                }
            }, "json");
        }
    });

    function closeView() {
        layer.close(LayWin['wPersonnel']);
    }

    function findPersonnel() {
        if ($.trim($("#mobile").val()) == "") {
            return false;
        }

        $.ajax({
            url: "<c:url value="/web/admin/personnel/findUserByMobile.action"/>",
            type: 'GET',
            dataType: 'JSON',
            data: {mobile: $("#mobile").val()},
            success: function (result) {
                if (result.status === 0) {
                    var data = result.data;
                    if(data.organizationId!="") {
                        organizationId.setValue([data.organizationId]);
                    }else {
                        positionId.clear();
                    }
                   // $("#organizationId").val();
                    $("#userName").val(data.name);
                    if(data.sex==='女'){
                        $("#sex").val(data.sex);
                    }
                    $("#employeeCode").val(data.employeeCode);
                    $("#email").val(data.email);
                    if(data.positionId!=""){
                        positionId.setValue([data.positionId]);
                    }else {
                        positionId.clear();
                    }

                   // $("#positionId").val(data.positionId);
                    $('.selectpicker').selectpicker('val', data.areaIds);
                } else {
                    /*organizationId.clear();*/
                   // $("#organizationId").val("");
                    $("#userName").val("");
                    // $("#sex").val("");
                    $("#employeeCode").val("");
                    $("#email").val("");
                    positionId.clear();
                    //$("#positionId").val("");

                    $('.selectpicker').selectpicker('val', '');
                }
            }
        })
    }
</script>


